package com.chen.controller;


import com.chen.model.*;
import com.chen.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/contract")
public class ContractController {
    @Autowired
    private ContractService contractService;
    @Autowired
    private InvoiceService invoiceService;
    @Autowired
    private SalesmanService salesmanService;
    @Autowired
    private CustomerService customerService;
    @Autowired
    private GoodsService goodsService;

    @RequestMapping("/contract-add")
    public String contractAdd(Model model){
        List<Salesman> salesmanList= salesmanService.findAllSalesman();
        List<Customer> customerList= customerService.findAllCustomer();
        List<Goods> goodsList = goodsService.findAllGoods();
        model.addAttribute("salesmanList",salesmanList);
        model.addAttribute("customerList",customerList);
        model.addAttribute("goodsList",goodsList);
        return "contract-add";
    }

    @RequestMapping("/contract-edit")
    public String contractEdit(Integer contract_id,Model model) throws ParseException {
        Contract contract = contractService.findContractById(contract_id);
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        String contract_time=sdf.format(contract.getContract_time());
        model.addAttribute("contract",contract);
        model.addAttribute("contract_time",contract_time);
        return "contract-edit";
    }

    @RequestMapping("/updateContract")
    public String updateContract(Contract contract , HttpServletRequest request) throws ParseException {
        System.out.println(contract);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//yyyy-mm-dd, 会出现时间不对, 因为小写的mm是代表: 秒
        String time1 = request.getParameter("time");
        Date utilDate = sdf.parse(time1);
        contract.setContract_time(utilDate);
        contractService.updateContract(contract);
        return "success";
    }

    @RequestMapping("/addContract")
    public String addContract(Contract contract ,Integer customer_id,Integer salesman_id,Integer [] goods_number,Integer[] goods_price,Integer[] goods_id, HttpServletRequest request) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//yyyy-mm-dd, 会出现时间不对, 因为小写的mm是代表: 秒
        String time1 = request.getParameter("time");
        Date utilDate = sdf.parse(time1);
        Customer customer = customerService.findCustomerById(customer_id);
        Salesman salesman = salesmanService.findSalesmanById(salesman_id);
        contract.setSalesman(salesman);
        contract.setCustomer(customer);
        contract.setContract_time(utilDate);
        System.out.println(contract.toString());
        contractService.addContract(contract);

        int contract_id = contract.getContract_id();
        System.out.println(contract_id);
        for (int i=0 ; i<goods_number.length;i++) {
            if(goods_number[i]!=0) {
                Invoice invoice = new Invoice();
                Goods goods = new Goods();
                Contract contract1 = new Contract();
                goods.setGoods_id(goods_id[i]);
                contract1.setContract_id(contract_id);
                invoice.setGoods_number(goods_number[i]);
                invoice.setGoods_price(goods_price[i]);
                invoice.setInvoice_state(0);
                invoice.setInvoice_time(utilDate);
                invoice.setContract(contract1);
                invoice.setGoods(goods);
                invoiceService.addInvoice(invoice);
            }
        }
        return "success";
    }

    @RequestMapping("/test")
//    @ResponseBody
    public void test(){
        Contract contract = new Contract();
        Customer customer = new Customer();
        Salesman salesman = new Salesman();
        customer.setCustomer_id(1);
        salesman.setSalesman_id(1);
        contract.setCustomer(customer);
        contract.setSalesman(salesman);
        contract.setContract_message("123");
        contract.setContract_state(0);
        contract.setContract_time(new Date());
        System.out.println(contract);
        contractService.addContract(contract);
    }

    @RequestMapping("/contract_check")
    public String contract_check(Model model,Integer contract_id){
        List<Invoice> invoiceList;
        System.out.println("111");
        System.out.println(contract_id.toString()+"111");
//        invoiceList= invoiceService.findInvoiceByContract(contract_id);
        invoiceList= invoiceService.findInvoiceByContract(contract_id);
//        invoiceList= invoiceService.findAllInvoice();
        System.out.println(invoiceList.toString());

        model.addAttribute("invoiceList",invoiceList);
        return "contract_check";
    }

    @RequestMapping("/findAllContract")
    public String findAllContract(Model model){
        List<Contract> contractList;
        contractList = contractService.findAllContract();
        for (Contract contract :contractList){
            List<Invoice> invoiceList = invoiceService.findInvoiceByContract(contract.getContract_id());
            int state = 2;
            for (Invoice invoice:invoiceList){
                if(invoice.getInvoice_state()==1){
                    state=1;
                    break;
                }
            }
            if(state==1) {
                contract.setContract_state(state);
                contractService.updateState(contract);
                continue;
            }
            for (Invoice invoice:invoiceList){
                if(invoice.getInvoice_state()!=2){
                    state=0;
                    break;
                }
            }
            if(state==0){
                contract.setContract_state(state);
                contractService.updateState(contract);
                continue;
            }
            else {
                contract.setContract_state(state);
                contractService.updateState(contract);
            }
//            System.out.println(contract);
        }
        contractList = contractService.findAllContract();
        model.addAttribute("contractList",contractList);
        return "contract-list";
    }

    @RequestMapping("/findContractById")
    public String  findContractById(Integer contract_id,Model model){
        Contract contract = contractService.findContractById(contract_id);
        List<Contract> contractList = new ArrayList<>();
        if (contract!=null)
            contractList.add(contract);
        model.addAttribute("contractList",contractList);
        return "contract-list";

    }
}
